<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { register } from '../services/auth';

const router = useRouter();
const username = ref('');
const password = ref('');
const confirmPassword = ref('');
const email = ref('');
const errors = ref({});
const isLoading = ref(false);
const successMessage = ref('');

// 验证表单
const validateForm = () => {
  errors.value = {};
  let isValid = true;
  
  // 用户名验证
  if (!username.value) {
    errors.value.username = '请输入用户名';
    isValid = false;
  } else if (username.value.length < 3 || username.value.length > 20) {
    errors.value.username = '用户名长度应在3-20个字符之间';
    isValid = false;
  }
  
  // 邮箱验证
  if (!email.value) {
    errors.value.email = '请输入邮箱';
    isValid = false;
  } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.value)) {
    errors.value.email = '请输入有效的邮箱地址';
    isValid = false;
  }
  
  // 密码验证
  if (!password.value) {
    errors.value.password = '请输入密码';
    isValid = false;
  } else if (password.value.length < 6) {
    errors.value.password = '密码长度至少为6个字符';
    isValid = false;
  }
  
  // 确认密码验证
  if (!confirmPassword.value) {
    errors.value.confirmPassword = '请确认密码';
    isValid = false;
  } else if (confirmPassword.value !== password.value) {
    errors.value.confirmPassword = '两次输入的密码不一致';
    isValid = false;
  }
  
  return isValid;
};

const handleRegister = async () => {
  successMessage.value = '';
  
  if (!validateForm()) {
    return;
  }
  
  isLoading.value = true;
  
  try {
    const success = await register({
      username: username.value,
      password: password.value,
      email: email.value
    });
    
    if (success) {
      successMessage.value = '注册成功！正在跳转登录页面...';
      // 3秒后跳转到登录页
      setTimeout(() => {
        router.push('/login');
      }, 3000);
    } else {
      errors.value.general = '注册失败，请稍后重试';
    }
  } catch (err) {
    errors.value.general = '注册失败，请稍后重试';
    console.error('Register error:', err);
  } finally {
    isLoading.value = false;
  }
};

const goToLogin = () => {
  router.push('/login');
};
</script>

<template>
  <div class="register-container">
    <div class="register-form">
      <h2 class="register-title">注册账号</h2>
      
      <!-- 成功提示 -->
      <div v-if="successMessage" class="success-message">
        {{ successMessage }}
      </div>
      
      <!-- 通用错误提示 -->
      <div v-if="errors.general" class="error-message general-error">
        {{ errors.general }}
      </div>
      
      <!-- 注册表单 -->
      <form @submit.prevent="handleRegister" :disabled="isLoading">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            id="username"
            v-model="username"
            type="text"
            placeholder="请输入用户名"
            :disabled="isLoading"
          />
          <div v-if="errors.username" class="error-text">
            {{ errors.username }}
          </div>
        </div>
        
        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            id="email"
            v-model="email"
            type="email"
            placeholder="请输入邮箱"
            :disabled="isLoading"
          />
          <div v-if="errors.email" class="error-text">
            {{ errors.email }}
          </div>
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            id="password"
            v-model="password"
            type="password"
            placeholder="请输入密码"
            :disabled="isLoading"
          />
          <div v-if="errors.password" class="error-text">
            {{ errors.password }}
          </div>
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            id="confirmPassword"
            v-model="confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            :disabled="isLoading"
          />
          <div v-if="errors.confirmPassword" class="error-text">
            {{ errors.confirmPassword }}
          </div>
        </div>
        
        <button 
          type="submit" 
          class="register-button"
          :disabled="isLoading"
        >
          {{ isLoading ? '注册中...' : '注册' }}
        </button>
      </form>
      
      <!-- 登录链接 -->
      <div class="login-link">
        已有账号？<button @click="goToLogin" class="link-button">立即登录</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 120px);
  padding: 40px 20px;
  background-color: var(--background-color);
}

.register-form {
  background-color: var(--white);
  border-radius: var(--border-radius);
  padding: 40px;
  width: 100%;
  max-width: 450px;
  box-shadow: var(--shadow-md);
}

.register-title {
  font-size: 28px;
  font-weight: 600;
  color: var(--text-primary);
  text-align: center;
  margin-bottom: 30px;
}

.success-message {
  background-color: #efe;
  color: #3c3;
  padding: 12px;
  border-radius: var(--border-radius);
  margin-bottom: 20px;
  font-size: 14px;
  text-align: center;
}

.error-message {
  background-color: #fee;
  color: #c33;
  padding: 12px;
  border-radius: var(--border-radius);
  margin-bottom: 20px;
  font-size: 14px;
}

.general-error {
  text-align: center;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  font-size: 16px;
  transition: border-color 0.3s ease;
  background-color: var(--white);
}

.form-group input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(251, 114, 153, 0.1);
}

.form-group input.error {
  border-color: #c33;
}

.error-text {
  color: #c33;
  font-size: 12px;
  margin-top: 4px;
}

.register-button {
  width: 100%;
  padding: 14px;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: var(--border-radius);
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s ease;
  margin-top: 10px;
}

.register-button:hover:not(:disabled) {
  background-color: #e86387;
}

.register-button:disabled {
  background-color: var(--text-light);
  cursor: not-allowed;
}

.login-link {
  text-align: center;
  margin-top: 24px;
  font-size: 14px;
  color: var(--text-secondary);
}

.link-button {
  background: none;
  border: none;
  color: var(--primary-color);
  font-weight: 500;
  cursor: pointer;
  font-size: 14px;
  padding: 0;
  margin-left: 4px;
}

.link-button:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    padding: 20px;
  }
  
  .register-form {
    padding: 30px 24px;
  }
  
  .register-title {
    font-size: 24px;
    margin-bottom: 24px;
  }
}

@media (max-width: 480px) {
  .register-form {
    padding: 24px 20px;
  }
  
  .form-group input {
    padding: 10px;
    font-size: 15px;
  }
  
  .register-button {
    padding: 12px;
    font-size: 15px;
  }
}
</style>